
import time
from threading import Thread
import redis


# # 函数描述线程任务
# def task():
#     print("子线程开始执行...")
#     time.sleep(3)
#     print("子线程执行结束...")
#
#
# if __name__ == '__main__':
#     print("主线程开始执行...")
#     t = Thread(target=task)  # 创建线程对象
#     t.start()  # 启动线程
#     t.join()  # 阻塞主线程，等待子线程执行完毕
#     print("主线程执行结束...")

# # 类描述线程任务
# class MyThread(Thread):
#
#     def __init__(self):
#         super().__init__()
#
#     def run(self):  # 重写run方法
#         print("子线程开始执行...")
#         time.sleep(3)
#         print("子线程执行结束...")
#
#
# if __name__ == '__main__':
#     print("主线程开始执行...")
#     t = MyThread()  # 创建线程对象
#     t.start()  # 启动线程
#     t.join()  # 阻塞主线程，等待子线程执行完毕
#     print("主线程执行结束...")

# # 线程任务传参
# def task(a, b, c, d):
#     print("子线程开始执行...")
#     print("参数：", a, b, c, d)
#     time.sleep(3)
#     print("子线程执行结束...")
#
#
# if __name__ == '__main__':
#     print("主线程开始执行...")
#     t = Thread(target=task, args=(1, 2), kwargs={"c": 3, "d": 4})  # 创建线程对象
#     t.start()  # 启动线程
#     t.join()  # 阻塞主线程，等待子线程执行完毕
#     print("主线程执行结束...")

# # 多线程之间进行通信 : 队列，全局变量，redis
# from queue import Queue, LifoQueue, PriorityQueue
# from threading import Thread
#
#
# def write_thread():
#     global redis_conn
#     redis_conn.set("name", "张三")
#
#
# def read_thread():
#     global redis_conn
#     print(redis_conn.get("name"))
#
#
# if __name__ == '__main__':
#     redis_conn = redis.Redis()
#     write_thread = Thread(target=write_thread)
#     write_thread.start()
#     write_thread.join()
#
#     read_thread = Thread(target=read_thread)
#     read_thread.start()
#     read_thread.join()


# 线程锁   协程
# from threading import Lock
#
#
# def task():
#     global num
#     for i in range(100000):
#         lock.acquire()
#         num += 1
#         lock.release()
#     print(num)
#
#
# if __name__ == '__main__':
#     num = 0
#     lock = Lock()
#     t1 = Thread(target=task)
#     t2 = Thread(target=task)
#     t1.start()
#     t2.start()
#     t1.join()
#     t2.join()

# # 守护线程
# def task():
#     print("2.子线程开始执行...")
#     time.sleep(3)
#     print("3.子线程执行结束...")
#
#
# if __name__ == '__main__':
#     print("1.主线程开始执行...")
#     t = Thread(target=task)  # 创建线程对象
#     t.daemon = True  # 设置为守护线程
#     t.start()  # 启动线程
#     # t.join() # 阻塞主线程，等待子线程执行完毕
#     print("4.主线程执行结束...")
